package com.iemes.controller.order;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.ClientFormMap;
import com.iemes.entity.ClientPlannerFormMap;
import com.iemes.entity.CopyMoldFormMap;
import com.iemes.entity.EnterstorageFormMap;
import com.iemes.entity.EntryFormMap;
import com.iemes.entity.OrderFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.client.ClientMapper;
import com.iemes.mapper.entry.EntryMapper;
import com.iemes.mapper.mould.CopyMoldMapper;
import com.iemes.mapper.order.OrderMapper;
import com.iemes.mapper.planner.ClientPlannerMapper;
import com.iemes.mapper.warehouse.EnterstorageMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

@RequestMapping("/order/")
@Controller
public class OrderSelController extends BaseController {
	@Inject
	private OrderMapper orderMapper;
	@Inject
	private ClientMapper clientMapper;
	@Inject
	private UserMapper userMapper;
	@Inject
	private ClientPlannerMapper clientPlannerMapper;
	@Inject
	private CopyMoldMapper copyMoldMapper;
	@Inject
	private EnterstorageMapper enterstorageMapper;
	// 查看订单
	@RequestMapping("examineOrder")
	public String examineOrder(Model model) {
		List<ClientFormMap> clientFormMaps = clientMapper
				.findByWhere(new ClientFormMap());
		model.addAttribute("client", clientFormMaps);
		List<UserFormMap> userFormMaps = userMapper
				.findByWhere(new UserFormMap());
		model.addAttribute("alluser", userFormMaps);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/order/order_list";
	}

	// 订单分页
	@ResponseBody
	@RequestMapping("order_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {

		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
		orderFormMap = toFormMap(orderFormMap, pageNow, pageSize,
				orderFormMap.getStr("orderby"));
		orderFormMap.put("order_state", 1);
		orderFormMap.put("delete_state", 0);
		orderFormMap.put("parent_id", 0);
		if (!userFormMap.getStr("site").equals("*")) {
			if (userFormMap.getInt("position_id") == 3) {
				orderFormMap.put("salesman_id", userFormMap.getInt("id"));
			}
			if (userFormMap.getInt("position_id") == 2) {
				orderFormMap.put("planner_id", userFormMap.getInt("id"));
			}
		}
		List<OrderFormMap> orderFormMaps = orderMapper.findByPage(orderFormMap);
		List<OrderFormMap> orderclient = orderMapper.getAllName();
		for (int i = 0; i < orderFormMaps.size(); i++) {
			for (int j = 0; j < orderclient.size(); j++) {
				if (orderFormMaps.get(i).get("client_no") == orderclient.get(j)
						.get("client_no")) {
					orderFormMaps.get(i).put("clientname",
							orderclient.get(j).get("clientname"));
				}
				if (orderFormMaps.get(i).get("salesman_id") == orderclient.get(
						j).getInt("salesman_id")) {
					orderFormMaps.get(i).put("salesman",
							orderclient.get(j).get("salesman"));
				}
				if (orderFormMaps.get(i).get("planner_id") == orderclient
						.get(j).getInt("planner_id")) {
					orderFormMaps.get(i).put("planner",
							orderclient.get(j).get("planner"));
				}
				if (orderFormMaps.get(i).get("byuser_id") == orderclient.get(j)
						.getInt("byuser_id")) {
					orderFormMaps.get(i).put("byuser",
							orderclient.get(j).get("byuser"));
				}
			}

		}
		pageView.setRecords(orderFormMaps);
		return pageView;
	}

	// 编辑订单
	@RequestMapping("order_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		// 获取所有客户
		List<ClientFormMap> clientFormMap = clientMapper
				.findByWhere(new ClientFormMap());
		model.addAttribute("client", clientFormMap);
		List<UserFormMap> userFormMaps = userMapper
				.findByWhere(new UserFormMap());
		model.addAttribute("alluser", userFormMaps);
		if (Common.isNotEmpty(id)) {
			OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
					OrderFormMap.class);
			List<OrderFormMap> orderclient = orderMapper.getAllName();
			for (int i = 0; i < orderclient.size(); i++) {
				if (orderFormMap.getInt("client_no") == orderclient.get(i)
						.getInt("client_no")) {
					orderFormMap.put("clientname",
							orderclient.get(i).getStr("clientname"));
				}
				if (orderFormMap.getInt("salesman_id") == orderclient.get(i)
						.getInt("salesman_id")) {
					orderFormMap.put("salesman",
							orderclient.get(i).getStr("salesman"));
				}
				if (orderFormMap.getInt("planner_id") == orderclient.get(i)
						.getInt("planner_id")) {
					orderFormMap.put("planner",
							orderclient.get(i).getStr("planner"));
				}
			}

			model.addAttribute("order", orderFormMap);
		}

		return Common.BACKGROUND_PATH + "/order/order_edit";
	}

	// 修改订单
	@ResponseBody
	@RequestMapping(value = "order_editEntity")
	@SystemLog(module = "订单数据维护", methods = "查看订单-修改订单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() {
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
		try {
			orderMapper.editEntity(orderFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "false";
		}
		return "success";
	}

	// 新增订单页面
	@RequestMapping("order_add")
	public String addUI(Model model) throws Exception {
		String id = getPara("id");
		OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
				OrderFormMap.class);
		List<OrderFormMap> orderclient = orderMapper.getAllName();
		for (int i = 0; i < orderclient.size(); i++) {
			if (orderFormMap.getInt("client_no") == orderclient.get(i).getInt(
					"client_no")) {
				orderFormMap.put("clientname",
						orderclient.get(i).getStr("clientname"));
			}
			if (orderFormMap.getInt("salesman_id") == orderclient.get(i)
					.getInt("salesman_id")) {
				orderFormMap.put("salesman",
						orderclient.get(i).getStr("salesman"));
			}
			if (orderFormMap.getInt("planner_id") == orderclient.get(i).getInt(
					"planner_id")) {
				orderFormMap.put("planner", orderclient.get(i)
						.getStr("planner"));
			}
		}

		model.addAttribute("order", orderFormMap);
		return Common.BACKGROUND_PATH + "/order/order_add";
	}

	// 新增订单操作
	@ResponseBody
	@RequestMapping("order_addEntity")
	@SystemLog(module = "订单管理", methods = "订单管理-新增订单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() {
		// JSONObject json = new JSONObject();
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		orderFormMap.put("byuser_id", userFormMap.getInt("id"));
		try {
			orderMapper.addEntity(orderFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "false";
		}
		return "success";
	}

	// 删除订单操作
	@ResponseBody
	@RequestMapping("order_deleteEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "订单管理", methods = "订单管理-删除订单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() {
		String[] ids = getParaValues("ids");
		String Pid = null;
		for (String id : ids) {
			Pid = id;
			// orderMapper.deleteOrder(Integer.valueOf(id));
		}
		String[] value = Pid.split(",");
		for (String string : value) {
			orderMapper.deleteOrder(Integer.valueOf(string));
		}
		return "success";
	}

	// 今日新订单
	@RequestMapping("NewOrder")
	public String NewOrder(Model model) {
		List<ClientFormMap> clientFormMaps = clientMapper
				.findByWhere(new ClientFormMap());
		model.addAttribute("client", clientFormMaps);
		List<UserFormMap> userFormMaps = userMapper
				.findByWhere(new UserFormMap());
		model.addAttribute("alluser", userFormMaps);
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/order/order_newlist";
	}

	// 对订单进行评估
	@RequestMapping("neworder_edit")
	public String neworder_edit(Model model) {
		String id = getPara("id");
		// 获取点击的订单
		OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
				OrderFormMap.class);
		String client_no = orderFormMap.getInt("client_no").toString();
		ClientFormMap clientFormMap = clientMapper.findbyFrist("id", client_no,
				ClientFormMap.class);
		List<OrderFormMap> orderclient = orderMapper.getAllName();
		for (int i = 0; i < orderclient.size(); i++) {
			if (orderFormMap.getInt("client_no") == orderclient.get(i).getInt(
					"client_no")) {
				orderFormMap.put("clientname",
						orderclient.get(i).getStr("clientname"));
			}
			if (orderFormMap.getInt("salesman_id") == orderclient.get(i)
					.getInt("salesman_id")) {
				orderFormMap.put("salesman",
						orderclient.get(i).getStr("salesman"));
			}
			if (orderFormMap.getInt("planner_id") == orderclient.get(i).getInt(
					"planner_id")) {
				orderFormMap.put("planner", orderclient.get(i)
						.getStr("planner"));
			}
		}
		model.addAttribute("client", clientFormMap);
		model.addAttribute("order", orderFormMap);
		return Common.BACKGROUND_PATH + "/order/neworder_edit";
	}

	// 订单分页
	@ResponseBody
	@RequestMapping("neworder_findByPage")
	public PageView newfindByPage(String pageNow, String pageSize) throws Exception {
		List<Map<String, Object>> orderFormMaps=orderMapper.getNewOrderList();
		for (Map<String, Object> map : orderFormMaps) {
			EnterstorageFormMap enterstorageFormMap=enterstorageMapper.findbyFrist("orderId", map.get("id").toString(), EnterstorageFormMap.class);
			
			if(enterstorageFormMap!=null){
				//剩余报数
				if(enterstorageFormMap.get("restCount")==null){
					map.put("cangkNum", 0);
				}else{
					Integer restCount=Integer.valueOf(enterstorageFormMap.get("restCount").toString());
					Integer orderPack=Integer.valueOf(enterstorageFormMap.get("orderPack").toString());
					map.put("cangkNum", restCount*orderPack);
				}
			}else{
				map.put("cangkNum", 0);
			}
		}
		LuogicPaging(pageNow, pageSize, orderFormMaps);
		return pageView;
	}

	// 新增订单操作
	@ResponseBody
	@RequestMapping("neworder_received")
	@SystemLog(module = "今日订单", methods = "今日订单-确接收订单")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String neworder_received() {
		// JSONObject json = new JSONObject();
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		orderFormMap.put("byuser_id", userFormMap.getInt("id"));
		orderFormMap.put("order_state", 1);
		orderFormMap.put("receiving_date", DateUtils.getStringDateTime());
		try {
			orderMapper.editEntity(orderFormMap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			return "false";
		}
		return "success";
	}

	// 编辑订单
	@RequestMapping("order_planner")
	public String order_planner(Model model) throws Exception {
		String id = getPara("id");
		// 获取所有客户
		OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
				OrderFormMap.class);
		List<ClientPlannerFormMap> clientPlannerFormMap = clientPlannerMapper
				.findByAttribute("order_id", id, ClientPlannerFormMap.class);
		ClientFormMap clientFormMap = clientMapper.findbyFrist("id",
				orderFormMap.get("client_no").toString(), ClientFormMap.class);
		orderFormMap.put("clientname", clientFormMap.get("name"));
		model.addAttribute("clientplanner", clientPlannerFormMap);
		model.addAttribute("order", orderFormMap);
		return Common.BACKGROUND_PATH + "/order/order_planner";
	}

	// 拆分订单页面
	@RequestMapping("order_chai")
	public String order_chai(Model model) throws Exception {
		String id = getPara("id");
		// 获取所有客户
		OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
				OrderFormMap.class);
		List<CopyMoldFormMap> copyMoldFormMaps = copyMoldMapper
				.findByAttribute("mold_code",
						orderFormMap.getStr("mold_encode"),
						CopyMoldFormMap.class);
		model.addAttribute("order", orderFormMap);
		model.addAttribute("copyMold", copyMoldFormMaps);
		return Common.BACKGROUND_PATH + "/order/order_chai";
	}
	// 拆分订单详细页面
		@RequestMapping("order_chaiinfo")
		public String order_chaiinfo(Model model) throws Exception {
			String id = getPara("id");
			// 获取所有客户
			OrderFormMap orderFormMap = orderMapper.findbyFrist("id", id,
					OrderFormMap.class);
			List<CopyMoldFormMap> copyMoldFormMaps = copyMoldMapper
					.findByAttribute("mold_code",
							orderFormMap.getStr("mold_encode"),
							CopyMoldFormMap.class);
			List<OrderFormMap> orderFormMaps = orderMapper.findByAttribute(
					"parent_id", id, OrderFormMap.class);
			ClientFormMap clientFormMap = clientMapper.findbyFrist("id",
					orderFormMap.get("client_no").toString(), ClientFormMap.class);
			orderFormMap.put("clientname", clientFormMap.get("name"));
			model.addAttribute("order", orderFormMap);
			model.addAttribute("copyMold", copyMoldFormMaps);
			for (OrderFormMap order : orderFormMaps) {
				UserFormMap userFormMap=userMapper.findbyFrist("id",order.get("byuser_id").toString() , UserFormMap.class);
				order.put("username", userFormMap.get("userName"));
			}
			model.addAttribute("orderchai", orderFormMaps);
			return Common.BACKGROUND_PATH + "/order/order_chaiFormation";
		}

	// 拆分订单操作
	@RequestMapping("order_chaiEntity")
	@ResponseBody
	@Transactional
	public String order_chaiEntity(Model model, HttpServletRequest request) {
		String[] number = request.getParameterValues("chainumber");
		OrderFormMap orderFormMap = getFormMap(OrderFormMap.class);
		OrderFormMap order = orderMapper.findbyFrist("id",
				orderFormMap.get("id").toString(), OrderFormMap.class);
		
		Integer totalNumber = Integer.valueOf(request.getParameter("residue"));
		if (totalNumber == order.getInt("number")) {
				return "false";
		}
		try {
			orderMapper.deleteByAttribute("parent_id", orderFormMap.get("id").toString(), OrderFormMap.class);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return "false";
		}
		order.remove("id");
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for (int i = 0; i < number.length; i++) {
			// 把空的赋值为0
			if (i==number.length-1) {
				continue;
			}
			order.remove("id");
			order.put("number", number[i]);
			order.put("byuser_id",userFormMap.getInt("id"));
			order.put("parent_id", orderFormMap.get("id").toString());
			order.put("receiving_date", DateUtils.getStringDateTime());
			try {
				orderMapper.addEntity(order);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "false";
			}
		}
		// 最后把剩余数补进去
		if (totalNumber != 0) {
			order.remove("id");
			order.put("number", totalNumber);
			order.put("byuser_id",userFormMap.getInt("id"));
			order.put("parent_id", orderFormMap.get("id").toString());
			order.put("receiving_date", DateUtils.getStringDateTime());
			
			try {
				orderMapper.addEntity(order);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "false";
			}
		}
		return "success";
	}
}
